{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/shoremei/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import random\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True, False, False, False],\n",
       "       [ True,  True,  True, False],\n",
       "       [ True,  True,  True,  True]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,3,4]\n",
    "b = tf.sequence_mask(a)\n",
    "sess = tf.Session()\n",
    "sess.run(tf.global_variables_initializer())\n",
    "sess.run(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0.],\n",
       "       [1., 1., 1., 0.],\n",
       "       [1., 1., 1., 1.]], dtype=float32)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = tf.cast(b, dtype=tf.float32)\n",
    "sess.run(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 1, 4, 1)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = tf.expand_dims(c, -1)\n",
    "e = sess.run(d)\n",
    "e.shape\n",
    "f = tf.expand_dims(e, 1)\n",
    "g = sess.run(f)\n",
    "g.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.64254846 0.10232709 0.42298807 0.95755044]\n",
      " [0.7614569  0.89574552 0.07326926 0.10652463]\n",
      " [0.48060903 0.16206899 0.48323195 0.43977625]]\n",
      "[[0.64254844 0.         0.         0.        ]\n",
      " [0.7614569  0.8957455  0.07326926 0.        ]\n",
      " [0.48060903 0.162069   0.48323196 0.43977624]]\n"
     ]
    }
   ],
   "source": [
    "# 所有变量必须初始化\n",
    "# 操作必须run了才能返回一个variable,variable和np.array是同类型,可以进行相同的操作.\n",
    "# * 是element wise的乘积,即每个对应的点进行相乘.\n",
    "# 而matmul是矩阵乘积\n",
    "np_random = np.random.rand(3,4)\n",
    "print(np_random)\n",
    "tf_random = tf.Variable(np_random, dtype=tf.float32)\n",
    "sess.run(tf.global_variables_initializer())\n",
    "sess.run(tf_random)\n",
    "d=tf_random * c\n",
    "e = sess.run(d)\n",
    "print(e)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 3, 4, 6)\n"
     ]
    }
   ],
   "source": [
    "# tensorflow里的matmul对于高维张量时,除了最后两维度要满足矩阵的形状要求,其他的高维,必须相同.\n",
    "new_ran = tf.Variable(np.random.rand(3,3,4,3), dtype=tf.float32)\n",
    "new_ran2 = tf.Variable(np.random.rand(3,3,3,6), dtype=tf.float32)\n",
    "sess.run(tf.global_variables_initializer())\n",
    "matmul = tf.matmul(new_ran, new_ran2)\n",
    "rst = sess.run(matmul)\n",
    "print(rst.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
